package com.xuhao.examai.scoring.interfaces.controllers;

import com.xuhao.examai.scoring.application.services.ScoringService;
import com.xuhao.examai.scoring.application.dto.ExamStatistics;
import com.xuhao.examai.scoring.domain.models.AnswerRecord;
import com.xuhao.examai.scoring.domain.models.ExamRecord;
import com.xuhao.examai.common.ResultVO;
import com.xuhao.examai.common.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "评分管理", description = "评分相关API")
@RestController
@RequestMapping("/api/scoring")
@RequiredArgsConstructor
public class ScoringController {

	private final ScoringService scoringService;

	@Operation(summary = "评分单个答案")
	@PostMapping("/answers")
	public ResultVO<AnswerRecord> scoreAnswer(
			@RequestParam Long examRecordId,
			@RequestParam Long questionId,
			@RequestParam String studentAnswer) {
		return ResultVO.success(scoringService.scoreAnswer(examRecordId, questionId, studentAnswer));
	}

	@Operation(summary = "批量评分答案")
	@PostMapping("/answers/batch")
	public ResultVO<List<AnswerRecord>> batchScoreAnswers(
			@RequestParam Long examRecordId,
			@Valid @RequestBody List<AnswerRecord> answerRecords) {
		return ResultVO.success(scoringService.batchScoreAnswers(examRecordId, answerRecords));
	}

	@Operation(summary = "完成考试评分")
	@PostMapping("/exams/{examRecordId}/complete")
	public ResultVO<ExamRecord> completeExamScoring(@PathVariable Long examRecordId) {
		return ResultVO.success(scoringService.completeExamScoring(examRecordId));
	}

	@Operation(summary = "获取考试记录")
	@GetMapping("/exam-records/{examRecordId}")
	public ResultVO<ExamRecord> getExamRecord(@PathVariable Long examRecordId) {
		return ResultVO.success(scoringService.getExamRecord(examRecordId));
	}

	@Operation(summary = "获取答案记录列表")
	@GetMapping("/exam-records/{examRecordId}/answers")
	public ResultVO<List<AnswerRecord>> getAnswerRecords(@PathVariable Long examRecordId) {
		return ResultVO.success(scoringService.getAnswerRecords(examRecordId));
	}

	@Operation(summary = "获取学生考试记录")
	@GetMapping("/students/{studentId}/exam-records")
	public ResultVO<List<ExamRecord>> getStudentExamRecords(@PathVariable Long studentId) {
		return ResultVO.success(scoringService.getStudentExamRecords(studentId));
	}

	@Operation(summary = "分页查询考试记录")
	@GetMapping("/exam-records")
	public ResultVO<PageResult<ExamRecord>> getExamRecordsByPage(
			@RequestParam(defaultValue = "1") Integer current,
			@RequestParam(defaultValue = "10") Integer size,
			@RequestParam(required = false) Long examId,
			@RequestParam(required = false) Long studentId,
			@RequestParam(required = false) String status) {
		return ResultVO.success(scoringService.getExamRecordsByPage(current, size, examId, studentId, status));
	}

	@Operation(summary = "获取考试统计信息")
	@GetMapping("/exams/{examId}/statistics")
	public ResultVO<ExamStatistics> getExamStatistics(@PathVariable Long examId) {
		return ResultVO.success(scoringService.getExamStatistics(examId));
	}

	@Operation(summary = "重新评分考试")
	@PostMapping("/exams/{examRecordId}/rescore")
	public ResultVO<ExamRecord> rescoreExam(@PathVariable Long examRecordId) {
		return ResultVO.success(scoringService.rescoreExam(examRecordId));
	}

	@Operation(summary = "导出成绩报告")
	@GetMapping("/exam-records/{examRecordId}/report")
	public ResponseEntity<byte[]> exportScoreReport(@PathVariable Long examRecordId) {
		byte[] reportData = scoringService.exportScoreReport(examRecordId);

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", "score_report_" + examRecordId + ".txt");

		return ResponseEntity.ok()
				.headers(headers)
				.body(reportData);
	}
}